#include "odn_scene_Scene.h"

namespace ouden
{
	namespace scene
	{
		
		//Destructor 
		ODN_Scene::~ODN_Scene()
		{
			//Delete all the scene elements
		    while (sceneelements.size())
			{
				delete sceneelements.back();
				sceneelements.pop_back();
			}
	
			//Clear the lists of scene elements and objects
			sceneelements.clear();
			sceneobjects.clear();
		}
		
		//Update the scene 
		bool ODN_Scene::Update()
		{
			//Updates all scene elements 
			//NOTE: This updates all the joints, restrictions, springs, etc. of the physics models 
			for (int i=0;i<sceneelements.size();i++) {
				sceneelements[i]->Update();
			}	
			
			//Update all dynamic points of the physics models 
			updateDynamicPoints();
			
			
			
		}
		
		//Draws all scene elements 
		bool ODN_Scene::Draw()
		{
			for (int i=0;i<sceneelements.size();i++) {		
				sceneelements[i]->Draw();
			}
		}
		
		//Adds a new element to the scene 
		ODN_SceneElement* ODN_Scene::addElement(ODN_SceneElement* e)
		{
			//Add the new element to the array 
			sceneelements.push_back(e);
			//Return a pointer back to the element 
			return e;
		}
		
		//Adds a new dynamic point to the scene 
		ODN_DynamicPoint* addDynamicPoint(ODN_DynamicPoint* p)
		{
			//Add the new dynamic point to the array 
			dynamicpoints.push_back(p);
			//Return a pointer back to the point 
			return p;
		}
		
		//Remove an element from the scene 
		void ODN_Scene::killElement(ODN_SceneElement* e)
		{
			//Create a new iterator to store the position of the element in the array 
			std::vector<ODN_SceneElement*>::iterator index;
			//Find the element in the array 
			index = std::find(sceneelements.begin(), sceneelements.end(), e);
			
			//TODO: Possibly look into finding a different way to do this
			
			//Delete the element
			delete e;
			e = 0;  //TODO: Make sure this is working
			//Remove the entity from the array
			sceneelements.erase(index);	
		}
		
	} //namespace scene 
} //namespace ouden 
